﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using System.Net;
using System.Net.Sockets;
using System.Threading;
using AquaControls;

namespace AfterDash
{
    public partial class AfterDash : Form
    {
        DashboardData dataReciever;
        RobotArm armControl;
        AxisCamera axisCamera;
        Logger logger;

        public AfterDash()
        {
            InitializeComponent();
        }

        public void DisplayData(string stringData)
        {
                string[] specific_packets = stringData.Split('$');
                for (int i = 0; i < specific_packets.Length; i++)
                {
                    string[] packet = specific_packets[i].Split(':');
                    #region Jag Data
                    if (packet[0].Equals("jagdata"))
                    {
                        string[] jagdata = packet[1].Split(',');
                        jag1_temp.Invoke(new MethodInvoker(delegate { jag1_temp.Text = jagdata[00]; }));
                        jag1_vout.Invoke(new MethodInvoker(delegate { jag1_vout.Text = jagdata[01]; }));
                        jag1_vbus.Invoke(new MethodInvoker(delegate { jag1_vbus.Text = jagdata[02]; }));
                        jag1_curr.Invoke(new MethodInvoker(delegate { jag1_curr.Text = jagdata[03]; }));

                        jag2_temp.Invoke(new MethodInvoker(delegate { jag2_temp.Text = jagdata[04]; }));
                        jag2_vout.Invoke(new MethodInvoker(delegate { jag2_vout.Text = jagdata[05]; }));
                        jag2_vbus.Invoke(new MethodInvoker(delegate { jag2_vbus.Text = jagdata[06]; }));
                        jag2_curr.Invoke(new MethodInvoker(delegate { jag2_curr.Text = jagdata[07]; }));

                        jag3_temp.Invoke(new MethodInvoker(delegate { jag3_temp.Text = jagdata[08]; }));
                        jag3_vout.Invoke(new MethodInvoker(delegate { jag3_vout.Text = jagdata[09]; }));
                        jag3_vbus.Invoke(new MethodInvoker(delegate { jag3_vbus.Text = jagdata[10]; }));
                        jag3_curr.Invoke(new MethodInvoker(delegate { jag3_curr.Text = jagdata[11]; }));

                        jag4_temp.Invoke(new MethodInvoker(delegate { jag4_temp.Text = jagdata[12]; }));
                        jag4_vout.Invoke(new MethodInvoker(delegate { jag4_vout.Text = jagdata[13]; }));
                        jag4_vbus.Invoke(new MethodInvoker(delegate { jag4_vbus.Text = jagdata[14]; }));
                        jag4_curr.Invoke(new MethodInvoker(delegate { jag4_curr.Text = jagdata[15]; }));

                        //float lspeed = (float)Convert.ToDouble(jagdata[16]);
                        //leftSpeedDial.Invoke(new MethodInvoker(delegate { leftSpeedDial.Value = lspeed; }));
                        //float rspeed = (float)Convert.ToDouble(jagdata[17]);
                        //rightSpeedDial.Invoke(new MethodInvoker(delegate { rightSpeedDial.Value = rspeed; }));
                    }
                    #endregion
                    #region Tracking Data
                    else if (packet[0].Equals("trackdata"))
                    {
                        string[] trackdata = packet[1].Split(',');
                        logger.WriteLog(trackdata);
                        bool sensor = false;

                        if(trackdata[00].Equals("1"))
                             sensor = true;

                        fleft_volt.Invoke(new MethodInvoker(delegate { fleft_volt.Checked = sensor; sensor = false; }));

                        if (trackdata[01].Equals("1"))
                            sensor = true;

                        fmiddle_volt.Invoke(new MethodInvoker(delegate { fmiddle_volt.Checked = sensor; sensor = false; }));

                        if (trackdata[02].Equals("1"))
                            sensor = true;

                        fright_volt.Invoke(new MethodInvoker(delegate { fright_volt.Checked = sensor; sensor = false; }));

                        if (trackdata[03].Equals("1"))
                            sensor = true;

                        bleft_volt.Invoke(new MethodInvoker(delegate { bleft_volt.Checked = sensor; sensor = false; }));

                        if (trackdata[04].Equals("1"))
                            sensor = true;

                        bmiddle_volt.Invoke(new MethodInvoker(delegate { bmiddle_volt.Checked = sensor; sensor = false; }));

                        if (trackdata[05].Equals("1"))
                            sensor = true;

                        bright_volt.Invoke(new MethodInvoker(delegate { bright_volt.Checked = sensor; sensor = false; }));
                    }
                    #endregion
                    #region Arm Control
                    else if (packet[0].Equals("towKnob"))
                    {
                        double potval = Convert.ToDouble(packet[1])  * (5 / 3.3);
                        pot1_voltage.Invoke(new MethodInvoker(delegate { pot1_voltage.Text = potval.ToString(); }));
                    }
                    else if (packet[0].Equals("wristKnob"))
                    {
                        double potval = Convert.ToDouble(packet[1]) * (5 / 3.3);
                        pot2_voltage.Invoke(new MethodInvoker(delegate { pot2_voltage.Text = potval.ToString(); }));
                    }
                    #endregion
                    #region Minibot Armed
                    else if (packet[0].Equals("nuke"))
                    {
                        bool armed = false;

                        if (Convert.ToInt32(packet[1]) == 1)
                            armed = true;

                        minibotArmed.Invoke(new MethodInvoker(delegate { minibotArmed.Checked = armed; }));
                    }
                    #endregion
                    #region Claw Switch
                    else if (packet[0].Equals("claw"))
                    {
                        bool open = false;

                        armControl.SetClawAngle(0);
                        if (Convert.ToInt32(packet[1]) == 1)
                        {
                            open = true;
                            armControl.SetClawAngle(45);
                        }

                        clawOpened.Invoke(new MethodInvoker(delegate { clawOpened.Checked = open; }));
                       
                    }
                    #endregion 
                    #region Minibot Deployed
                    else if (packet[0].Equals("green"))
                    {
                        bool deployed = false;

                        if (Convert.ToInt32(packet[1]) == 1)
                            deployed = true;

                        minibotDeployed.Invoke(new MethodInvoker(delegate { minibotDeployed.Checked = deployed; }));
                    }
                    #endregion
                    #region Arm Values
                    else if (packet[0].Equals("tower"))
                    {
                        float potval = (float)Convert.ToDouble(packet[1]);
                        towerPot_voltage.Invoke(new MethodInvoker(delegate { towerPot_voltage.Text = potval.ToString(); }));
                        armControl.SetArmAngle(potval * 30);
                    }
                    else if (packet[0].Equals("wrist"))
                    {
                        float potval = (float)Convert.ToDouble(packet[1]);
                        wristPot_voltage.Invoke(new MethodInvoker(delegate { wristPot_voltage.Text = potval.ToString(); }));
                        armControl.SetWristAngle(potval * 37);
                    }
                    else if (packet[0].Equals("claw_pot"))
                    {
                        clawPot_voltage.Invoke(new MethodInvoker(delegate { clawPot_voltage.Text = packet[1]; }));
                    }
                    #endregion
                }
        }

        private void AfterDash_Load(object sender, EventArgs e)
        {
            this.BringToFront();
            this.Location = new Point(0, 0);
            dataReciever = new DashboardData(this);
            dataReciever.StartDashboard();

            armControl = new RobotArm(armPanel);
            armControl.UpdateArm();
            axisCamera = new AxisCamera(this);
            axisCamera.StartStream();

            logger = new Logger();
        }

        private void AfterDash_FormClosing(object sender, FormClosingEventArgs e)
        {
            logger.EndLog();
            dataReciever.StopDashboard();
            axisCamera.StopStream();
        }
    }
}
